package org.wisdom.ab.apis.assembler;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.wisdom.ab.domain.staff.entity.AbStaff;
import org.wisdom.ab.domain.staff.entity.AbStaffDepartment;
import org.wisdom.ab.domain.department.repository.DepartmentRepository;
import org.wisdom.ab.dto.StaffDto;
import org.wisdom.exception.BusinessException;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 成员组装器
 *
 * @author 张智研
 * @date 2022/05/23 09:42
 **/
@RequiredArgsConstructor
@Component
public class StaffAssembler {

    private final DepartmentRepository departmentRepository;

    /**
     * 成员dto转换成entity
     *
     * @author 张智研
     * @date 2022/05/23 09:54
     * @param staffDto
     *            成员dto
     * @return org.wisdom.ab.domain.entity.AbUser
     */
    public AbStaff staffDtoToEntity(StaffDto staffDto) {
        Assert.notNull(staffDto, "成员信息不能为null");
        String[] deptIds = staffDto.getDepartment();
        Integer[] isLeaderInDept = staffDto.getIsLeaderInDept();
        Integer[] orders = staffDto.getOrder();
        Assert.isTrue(deptIds.length == isLeaderInDept.length && deptIds.length == orders.length, "部门数据个数不一致！");
        AbStaff staff = BeanUtil.copyProperties(staffDto, AbStaff.class);
        List<AbStaffDepartment> staffDepartmentList = new ArrayList<>();
        int n = 0;
        for (String deptId : deptIds) {
            boolean isExists = this.departmentRepository.existsById(deptId);
            if (!isExists) {
                throw new BusinessException("部门{" + deptId + "}不存在!");
            }
            AbStaffDepartment staffDepartment = new AbStaffDepartment();
            staffDepartment.setDeptId(deptId);
            staffDepartment.setOrder(orders[n]);
            staffDepartment.setIsLeader(isLeaderInDept[n]);
            staffDepartmentList.add(staffDepartment);
            n++;
        }
        staff.setStaffDepartments(staffDepartmentList);
        return staff;
    }

    /**
     * 成员entity转换成dto
     *
     * @author 张智研
     * @date 2022/05/23 09:54
     * @param staff
     *            成员
     * @return org.wisdom.ab.dto.UserDto
     */
    public StaffDto staffEntityToDto(AbStaff staff) {
        StaffDto staffDto = BeanUtil.copyProperties(staff, StaffDto.class, "userDepartments");
        List<AbStaffDepartment> userDepartmentList = staff.getStaffDepartments();
        int n = staff.getStaffDepartments().size();
        staffDto.setDepartment(new String[n]);
        staffDto.setOrder(new Integer[n]);
        staffDto.setIsLeaderInDept(new Integer[n]);
        int m = 0;
        for (AbStaffDepartment userDepartment : userDepartmentList) {
            staffDto.getDepartment()[m] = userDepartment.getDeptId();
            staffDto.getOrder()[m] = userDepartment.getOrder();
            staffDto.getIsLeaderInDept()[m] = userDepartment.getIsLeader();
            m++;
        }
        if(userDepartmentList.size() > 0) {
            staffDto.setDepartmentId(userDepartmentList.get(0).getDeptId());
        }
        return staffDto;
    }

    /**
     * 成员entity列表转换为dto列表
     *
     * @author 张智研
     * @date 2022/05/23 09:55
     * @param staffList
     *            成员列表
     * @return java.util.List<org.wisdom.ab.dto.UserDto>
     */
    public List<StaffDto> staffEntityToDto(List<AbStaff> staffList) {
        return staffList.stream().map(this::staffEntityToDto).collect(Collectors.toList());
    }

}
